<?php
defined("IN_IA") or exit("Access Denied");
include IA_ROOT . "/addons/yzzc_sun_plugin_distribution/func/func.php";
class yzzc_sun_plugin_distributionModuleWxapp extends WeModuleWxapp
{
    function return_msg($code = 1, $msg = "success",$data = array())
    {
        $res["code"] = $code;
        $res["data"] = $data;
        $res["msg"] = $msg;
        exit(json_encode($res));
    }
    /**
     * @return void
     * 检测是否是分销商
     */
    public function doPageIsPromoter()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        $status = intval($_GPC["status"]);
        if (empty($openid)) {
            $this->return_msg(-1,'openid不存在');
        }
        if (!pdo_tableexists("yzzc_sun_distribution_set")) {
            $this->return_msg(-1,[],'未安装插件');
            echo 9;
            exit;
        }
        $set = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "openid" => $openid));
        if ($status > 0) {
            if ($status == 3) {
                if ($set) {
                    if ($set["status"] != 1) {
                        echo 9;
                        exit;
                    } else {
                        echo 1;
                        exit;
                    }
                } else {
                    $info = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $uniacid));
                    if ($info["share_condition"] == 0 || $info["share_condition"] == 2) {
                        $data["uniacid"] = $uniacid;
                        $data["openid"] = $openid;
                        $data["form_id"] = $_GPC["form_id"];
                        $data["uid"] = $_GPC["uid"];
                        $data["addtime"] = time();
                        if ($info["share_condition"] == 2) {
                            $data["status"] = 1;
                        }
                        $user = pdo_get("yzzc_sun_user", array("uniacid" => $uniacid, "openid" => $openid), array("parents_name", "parents_id"));
                        $data["referrer_name"] = $user["parents_name"];
                        $data["referrer_uid"] = $user["parents_id"];
                        $res = pdo_insert("yzzc_sun_distribution_promoter", $data);
                        if ($res) {
                            echo $info["share_condition"];
                            if ($data["status"] == 1 && $info["share_condition"] == 2) {
                                $status = array("审核中", "已通过", "被拒绝");
                                $haveformid = $_GPC["form_id"];
                                $tpldata = array();
                                $tpldata["status"] = $status[$res["status"]];
                                sendtelmessage($res["openid"], "tpl_share_check", $haveformid, $tpldata);
                            }
                            exit;
                        } else {
                            echo 9;
                            exit;
                        }
                    }
                }
            }
            if ($set["status"] != 1) {
                echo 9;
                exit;
            }
        }
        if ($set) {
            $set["code_img"] = '';
            echo json_encode($set);
        } else {
            echo 9;
        }
        exit;
    }

    /**
     * @return void
     * 分销商个人详情
     */
    public function doPagePromoterInfo()
    {

        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        if (empty($openid)) {
            $this->return_msg(-1,'openid不存在');
        }
        if (!pdo_tableexists("yzzc_sun_distribution_set")) {
            $this->return_msg(-1,'未安装插件');
        }
        $set = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "openid" => $openid));
        if (!$set){
            $this->return_msg(-1,'销售不存在');
        }
        $this->return_msg(1,'操作成功',$set);
    }

    public function doPageGetUser()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        $user = pdo_get("yzzc_sun_user", array("uniacid" => $uniacid, "openid" => $openid), array("parents_name", "telphone"));
        $promoter = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "openid" => $openid), array("status"));
        $set = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $uniacid));
        $user["is_d_promoter"] = 0;
        if ($user) {
            if (!$user["parents_name"]) {
                $user["parents_name"] = '';
            } else {
                $user["parents_name"] = $user["parents_name"];
            }
            $user["mobile"] = $user["telphone"];
            if ($promoter) {
                $user["is_d_promoter"] = 1;
            }
            if ($promoter["status"] == 1) {
                $user["is_d_promoter"] = 1;
            } else {
                if ($set["share_condition"] == 2) {
                    $user["is_d_promoter"] = 1;
                } else {
                    if ($set["share_condition"] == 0) {
                        $user["is_d_promoter"] = 1;
                    } else {
                    }
                }
            }
            echo json_encode($user);
        } else {
            echo $user;
        }
        exit;
    }
    public function doPageSavePromoter()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        if (empty($openid)) {
            echo json_encode(array("code" => 0, "msg" => "没有登录请先登录"));
            exit;
        }
        $data["openid"] = $openid;
        $data["uniacid"] = $uniacid;
        $data["name"] = $_GPC["name"];
        $data["mobilephone"] = $_GPC["mobile"];
        $data["form_id"] = $_GPC["form_id"];
        $data["uid"] = $_GPC["uid"];
        $data["addtime"] = time();
        $user = pdo_get("yzzc_sun_user", array("uniacid" => $uniacid, "openid" => $openid), array("parents_name", "parents_id"));
        $data["referrer_name"] = $user["parents_name"];
        $data["referrer_uid"] = $user["parents_id"];
        $res = pdo_insert("yzzc_sun_distribution_promoter", $data);
        if ($res) {
            echo json_encode(array("code" => 1, "msg" => ''));
            exit;
        } else {
            return $this->return_msg(1, "申请失败！", array());
        }
    }
    public function doPageDistributionParsent()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        $parents_id = intval($_GPC["uid"]);
        if (empty($openid)) {
            echo 2;
            exit;
        }
        $user = pdo_get("yzzc_sun_user", array("uniacid" => $uniacid, "openid" => $openid), array("parents_id", "id"));
        if ($user["parents_id"] > 0) {
            echo 2;
            exit;
        }
        if ($user["id"] == $parents_id) {
            echo 2;
            exit;
        }
        $p_user_selft = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "uid" => $user["id"]), array("id"));
        if ($p_user_selft) {
            echo 2;
            exit;
        }
        $p_user = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "uid" => $parents_id), array("id"));
        if ($p_user) {
            $parsent_user = pdo_get("yzzc_sun_user", array("uniacid" => $uniacid, "id" => $parents_id), array("user_name", "id"));
            $data["parents_id"] = $parsent_user["id"];
            $data["parents_name"] = $parsent_user["user_name"];
            $res = pdo_update("yzzc_sun_user", $data, array("uniacid" => $_W["uniacid"], "id" => $user["id"]));
            echo 1;
            exit;
        } else {
            echo 2;
            exit;
        }
    }

    /**
     * @return void
     * 分销订单统计
     */
    public function doPageGetDistribution()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $uid = intval($_GPC["uid"]);
        $outdata = array();
        $data[":uniacid"] = $uniacid;
        $data[":uid"] = $uid;
        $nosettlemoney = 0;
        $allnum = 0;
        $d_set = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $_W["uniacid"]));
        if ($d_set["status"] > 0) {
            $nosettle = $this->CountOrdermoney(1, $uid);
            $nosettlemoney = $nosettle;
            $allnum = pdo_fetchcolumn("select count(id) as wname from " . tablename("yzzc_sun_user") . " where parents_id=" . $uid . " ");
            if ($d_set["status"] >= 2) {
                $nosettle = $this->CountOrdermoney(2, $uid);
                $nosettlemoney += $nosettle;
                $allnum += pdo_fetchcolumn("select count(id) as wname from " . tablename("yzzc_sun_user") . " where parents_id=ANY(SELECT id as user1 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=" . $uid . " and uniacid=" . $uniacid . ") and uniacid=" . $uniacid . "; ");
            }
            if ($d_set["status"] >= 3) {
                $nosettle = $this->CountOrdermoney(3, $uid);
                $nosettlemoney += $nosettle;
                $allnum += pdo_fetchcolumn("select count(id) as wname from " . tablename("yzzc_sun_user") . " where parents_id=ANY(SELECT id as user1 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=ANY(SELECT id as user1 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=" . $uid . " and uniacid=" . $uniacid . ") and uniacid=" . $uniacid . ") and uniacid=" . $uniacid . "; ");
            }
        }
        $outdata["nosettlemoney"] = $nosettlemoney;
        $outdata["allnum"] = $allnum;
        $sql = "select count(id) as num from " . tablename("yzzc_sun_distribution_order") . " where uniacid=:uniacid and (parent_id_1=:uid or parent_id_2=:uid or parent_id_3=:uid) ";
        $allorder = pdo_fetch($sql, $data);
        if ($allorder) {
            $outdata["allorder"] = $allorder["num"];
        } else {
            $outdata["allorder"] = 0;
        }
        if ($outdata) {
            echo json_encode($outdata);
        } else {
            echo 2;
        }
    }

    /**
     * @return void
     * 我的团队
     */
    public function doPageGetFirstLevel()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $uid = intval($_GPC["uid"]);
        $level = intval($_GPC["level"]);
        $data[":uniacid"] = $uniacid;
        $data[":uid"] = $uid;
        $pagesize = 10;
        $pageindex = intval($_GPC["page"]) * $pagesize;
        if ($level == 0) {
            $sql = "select id,user_name as name,headimg as img,time from " . tablename("yzzc_sun_user") . " where uniacid=:uniacid and parents_id=:uid order by id desc ";
        } else {
            if ($level == 1) {
                $sql = "SELECT user_name as name,headimg as img,time FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=ANY(SELECT id as user1 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=:uid and uniacid=:uniacid) and uniacid=:uniacid;";
            } else {
                if ($level == 2) {
                    $sql = "SELECT user_name as name,headimg as img,time FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=ANY(SELECT id as user1 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=ANY(SELECT id as user2 FROM " . tablename("yzzc_sun_user") . " WHERE parents_id=:uid and uniacid=:uniacid) and uniacid=:uniacid) and uniacid=:uniacid;";
                } else {
                }
            }
        }
        $sql = $sql . " limit " . $pageindex. "," . $pagesize;

        $alluser = pdo_fetchall($sql, $data);
        $this->return_msg(1,'操作成功',$alluser);
    }


    public function doPageSaveWithDraw()
    {
        global $_GPC, $_W;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        $uid = intval($_GPC["uid"]);
        $wd_type = intval($_GPC["wd_type"]);
        $money = $_GPC["money"];
        $account = $_GPC["account"];
        $uname = $_GPC["uname"];
        $phone = $_GPC["phone"];
        $formid = $_GPC["formid"];
        if (empty($uid) || empty($openid)) {
            return $this->return_msg(1, "参数错误", array());
        }
        if ($money <= 0) {
            return $this->return_msg(1, "提现金额必须大于0！", array());
        }
        $withdrawset = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $uniacid));
        if ($money < $withdrawset["minwithdraw"] && $withdrawset["minwithdraw"] > 0) {
            return $this->return_msg(1, "最低提现金额不得低于" . $withdrawset["minwithdraw"] . "元！", array());
        }
        if ($withdrawset["daymaxwithdraw"] > 0) {
            $starttime = strtotime(date("Y-m-d"));
            $endtime = strtotime(date("Y-m-d") . " 23:59:59");
            $withdrawmoney = pdo_fetchcolumn("select sum(money) as money from " . tablename("yzzc_sun_distribution_withdraw") . " where time between " . $starttime . " and " . $endtime . " and status < 2 and uniacid='" . $uniacid . "' ");
            if ($withdrawmoney + $money > $withdrawset["daymaxwithdraw"]) {
                return $this->return_msg(1, "超过当日最大提现额度，无法提现！", array());
            }
        }
        $promoter = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "uid" => $uid), array("canwithdraw", "freezemoney"));
        $canusemoney = $promoter["canwithdraw"] - $promoter["freezemoney"];
        if ($canusemoney < $money) {
            return $this->return_msg(1, "提现的金额必须比可提现余额少或相等！", array());
        }
        $rates = $withdrawset["withdrawhandingfee"];
        if ($rates > 0) {
            $ratesmoney = round($money * $rates / 100, 2);
        } else {
            $ratesmoney = 0;
        }
        $realmoney = round($money - $ratesmoney, 2);
        $wd_data = array();
        $data_brand_up["freezemoney"] = $promoter["freezemoney"] + $money;
        pdo_update("yzzc_sun_distribution_promoter", $data_brand_up, array("uniacid" => $uniacid, "uid" => $uid));
        $wd_data["status"] = 0;
        $wd_data["openid"] = $openid;
        $wd_data["uid"] = $uid;
        $wd_data["uniacid"] = $uniacid;
        $wd_data["uname"] = $uname;
        $wd_data["account"] = $account;
        $wd_data["withdrawaltype"] = $wd_type;
        $wd_data["time"] = time();
        $wd_data["mobilephone"] = $phone;
        $wd_data["money"] = $money;
        $wd_data["realmoney"] = $realmoney;
        $wd_data["ratesmoney"] = $ratesmoney;
        $wd_data["form_id"] = $formid;
        $res = pdo_insert("yzzc_sun_distribution_withdraw", $wd_data);
        $wd_id = pdo_insertid();
        echo 1;
    }
    public function doPageWithDraw()
    {
        global $_GPC, $_W;
        $uniacid = $_W["uniacid"];
        $openid = $_GPC["openid"];
        $uid = intval($_GPC["uid"]);
        $wd_type = intval($_GPC["wd_type"]);
        $money = $_GPC["money"];
        $account = $_GPC["account"];
        $formid = $_GPC["formid"];
        $user_info = pdo_get("yzzc_sun_distribution_promoter", array("uniacid" => $uniacid, "openid" => $openid));
        if (!$user_info){
            $this->return_msg(-1,'销售不存在');
        }
        if (empty($uid) || empty($openid)) {
            $this->return_msg(-1, "参数错误");
        }
        if ($money <= 0) {
            $this->return_msg(-1, "提现金额必须大于0！");
        }

        $withdraw = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $uniacid));
        if ($money < $withdraw["minwithdraw"] && $withdraw["minwithdraw"] > 0) {
            $this->return_msg(-1, "最低提现金额不得低于" . $withdraw["minwithdraw"] . "元！", array());
        }
        if ($withdraw["daymaxwithdraw"] > 0) {
            $starttime = strtotime(date("Y-m-d"));
            $endtime = strtotime(date("Y-m-d") . " 23:59:59");
            $withdrawmoney = pdo_fetchcolumn("select sum(money) as money from " . tablename("yzzc_sun_distribution_withdraw") . " where time between " . $starttime . " and " . $endtime . " and status < 2 and uniacid='" . $uniacid . "' ");
            if ($withdrawmoney + $money > $withdraw["daymaxwithdraw"]) {
                $this->return_msg(-1, "超过当日最大提现额度，无法提现！", array());
            }
        }

        $canusemoney = $user_info["canwithdraw"] - $user_info["freezemoney"];
        if ($canusemoney < $money) {
            $this->return_msg(-1, "提现的金额必须比可提现余额少或相等！", array());
        }
        $rates = $user_info["withdrawhandingfee"];
        if ($rates > 0) {
            $ratesmoney = round($money * $rates / 100, 2);
        } else {
            $ratesmoney = 0;
        }
        $realmoney = round($money - $ratesmoney, 2);
        $wd_data = [
            'status' => 0,
            'openid' => $openid,
            'uid' => $uid,
            'uniacid' => $uniacid,
            'uname' => $user_info['name'],
            'mobilephone' => $user_info['mobilephone'],
            'account' => $account,
            'withdrawaltype' => $wd_type,
            'time' => time(),
            'money' => $money,
            'realmoney' => $realmoney,
            'ratesmoney' => $ratesmoney,
            'form_id' => $formid,
        ];
        pdo_fetchall('START TRANSACTION');//启动一个新事务
        $data_brand_up["freezemoney"] = $user_info["freezemoney"] + $money;
        $res_update = pdo_update("yzzc_sun_distribution_promoter", $data_brand_up, array("uniacid" => $uniacid, "uid" => $uid));
        $res = pdo_insert("yzzc_sun_distribution_withdraw", $wd_data);
        if ($res_update && $res){
            pdo_fetchall('COMMIT');//事务提交
            $this->return_msg(1,'申请成功');
        }else{
            pdo_fetchall('ROLLBACK');//事务回滚
            $this->return_msg(-1,'申请失败');
        }
    }

    /**
     * @return void
     * 提现列表
     */
    public function doPageGetWithdrawal()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $uid = intval($_GPC["uid"]);
        $status = intval($_GPC["status"]);
        $data[":uniacid"] = $uniacid;
        $data[":uid"] = $uid;
        $pagesize = intval($_GPC["pagesize"]) ?? 10;
        $pageindex = (intval($_GPC["page"]) - 1) * $pagesize;
        if ($status != 'all') {
            $where = " and status=:status ";
            $data[":status"] = $status;
        }
        $sql = "select withdrawaltype,time,id,money,status from " . tablename("yzzc_sun_distribution_withdraw") . " where uniacid=:uniacid and uid=:uid " . $where . " order by id desc limit " . $pageindex . "," . $pagesize;
        $alluser = pdo_fetchall($sql, $data);
        if ($alluser){
            array_walk($alluser,function (&$v){
                $v['time'] = date('Y-m-d H:i:s',$v['time']);
            });
        }
        $this->return_msg(1,'获取成功',$alluser ?? []);
    }

    /**
     * @return void
     * 提现详情
     */
    public function doPageGetWithDrawDetail()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $id = intval($_GPC["id"]);
        $data = pdo_get("yzzc_sun_distribution_withdraw", array("uniacid" => $uniacid, "id" => $id));
        $data['time'] = date('Y-m-d H:i:s',$data['time']);
        if ($data) {
            $this->return_msg(1,'操作成功',$data);
        } else {
            $this->return_msg(-1,'提现不存在');
        }
    }

    function get_wx_qrcode($path,$width)
    {
        $data = ['path' => $path,'width'=>$width];
        $url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" . $this->getaccess_token();
        //向该URL，发送GET请求
        $result = $this->request_post($url, json_encode($data));
        if (!$result) {
            return false;
        }
        $res_obj = json_decode($result, true);
        if ($res_obj && !empty($res_obj['error_code'])) {
            $this->return_msg(-1,'生成小程序二维码失败');
        }
        return $result;
    }
    public function doPageGetWxCodeImg()
    {
        global $_W, $_GPC;
        $uid = $_GPC['uid'];
        $user_info = pdo_get('yzzc_sun_distribution_promoter',['uid'=>$uid]);
        if (!$user_info){
            $this->return_msg(-1,'销售不存在');
        }
        if (!$user_info['code_img']){
            $page = '/pages/index/index?parents_id='.$uid;
            $width = 430;
            $return = $this->get_wx_qrcode($page,$width);
//            $destDir = '/attachment/upload/wxapp_img';
//            if (!is_dir($destDir)) {
//                @mkdir($destDir, 755, true);
//            }
            $img = time() . rand(10000, 99999) . ".png";
            file_put_contents("../attachment/" . $img, $return);
            $img_path  = '/attachment/'.$img;
            pdo_update('yzzc_sun_distribution_promoter',['code_img'=>$img_path],['id'=>$user_info['id']]);
            $this->return_msg(1,'获取成功',$img_path);
        }else{
            $this->return_msg(1,'获取成功',$user_info['code_img']);
        }
    }

    public function doPageGetwxCode()
    {
        global $_W, $_GPC;
        $access_token = $this->getaccess_token();
        $scene = $_GPC["scene"];
        if (!preg_match("/[0-9a-zA-Z\\!\\#\\\$\\&'\\(\\)\\*\\+\\,\\/\\:\\;\\=\\?\\@\\-\\.\\_\\~]{1,32}/", $scene)) {
            return $this->return_msg(1, "场景值不合法", array());
        }
        $page = $_GPC["page"];
        $width = $_GPC["width"] ? $_GPC["width"] : 430;
        $auto_color = $_GPC["auto_color"] ? $_GPC["auto_color"] : false;
        $line_color = $_GPC["line_color"] ? $_GPC["line_color"] : array("r" => 0, "g" => 0, "b" => 0);
        $is_hyaline = $_GPC["is_hyaline"] ? $_GPC["is_hyaline"] : true;
        $uid = intval($_GPC["uid"]);
        $uniacid = $_W["uniacid"];
        if ($uid > 0) {
            $d_set = pdo_get("yzzc_sun_distribution_set", array("uniacid" => $uniacid), array("postertoppic", "postertoptitle"));
            if ($d_set) {
                $datas["blogo"] = !empty($d_set["postertoppic"]) ? $_W["attachurl"] . $d_set["postertoppic"] : $_W["siteroot"] . "/addons/yzzc_sun_plugin_distribution/images/shareimg.png";
                $datas["postertoptitle"] = $d_set["postertoptitle"];
            }
        }
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $access_token;
        $data = array();
        $data["scene"] = $scene;
        $data["page"] = $page;
        $data["width"] = $width;
        $data["auto_color"] = $auto_color;
        $data["line_color"] = $line_color;
        $data["is_hyaline"] = $is_hyaline;
        $json_data = json_encode($data);
        $return = $this->request_post($url, $json_data);
        $imgname = time() . rand(10000, 99999) . ".jpg";
        file_put_contents("../attachment/" . $imgname, $return);
        $datas["wxcode_pic"] = $imgname;
        echo json_encode($datas);
    }
    public function getaccess_token()
    {
        global $_W, $_GPC;
        $res = pdo_get("yzzc_sun_system", array("uniacid" => $_W["uniacid"]));
        $appid = $res["appid"];
        $secret = $res["appsecret"];
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . '';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        $data = json_decode($data, true);
        return $data["access_token"];
    }
    public function request_post($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $tmpInfo = curl_exec($ch);
        $error = curl_errno($ch);
        curl_close($ch);
        if ($error) {
            return false;
        } else {
            return $tmpInfo;
        }
    }
    public function doPageDelwxCode()
    {
        global $_W, $_GPC;
        $imgurl = $_GPC["imgurl"];
        $filename = "../attachment/" . $imgurl;
        if (file_exists($filename)) {
            $info = "删除成功";
            unlink($filename);
        } else {
            $info = "没找到:" . $filename;
        }
        echo $info;
    }
    public function doPagegetDistributionOrder()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $ordertype = isset($_GPC["ordertype"]) ? $_GPC["ordertype"] : 1;
        $status = intval($_GPC["orderstatus"]);
        $uid = intval($_GPC["uid"]);
        $pagesize = 10;
        $pageindex = isset($_GPC["page"]) ? intval($_GPC["page"]) : 0;
        $query = load()->object("query");
        $data = $query->from("yzzc_sun_order", "o")->leftjoin("yzzc_sun_distribution_order", "os")->on("o.id", "os.order_id")->leftjoin("yzzc_sun_user", "u")->on("u.id", "o.uid")->leftjoin("yzzc_sun_goods", "car")->on("o.cid", "car.id")->select("u.user_name as name", "u.headimg as uthumb", "o.ordernum", "o.money", "o.day", "o.id", "os.first_price", "os.second_price", "os.third_price", "car.pic as carimg", "car.name as carname", "o.cid", "o.prepay_money", "o.xzmoney", "o.status")->where("o.uniacid", $uniacid)->where("o.type", $ordertype)->where(function ($query) use($uid) {
            $query->whereor("os.parent_id_1", $uid);
            $query->whereor("os.parent_id_2", $uid);
            $query->whereor("os.parent_id_3", $uid);
        })->where(function ($q) use($status) {
            if ($status > 0) {
                goto CUaAc;
            }
            $q->where("o.id >", 0);
            goto F2H5L;
            CUaAc:
            if ($status == 2) {
                goto PGpYY;
            }
            if ($status == 3) {
                goto n0tKj;
            }
            if (!($status == 5)) {
                goto U6Ho0;
            }
            $q->where("o.status", 0);
            U6Ho0:
            goto NfPWe;
            n0tKj:
            $q->where("o.status >=", 2);
            $q->where("o.status <", 4);
            NfPWe:
            goto vMFLM;
            PGpYY:
            $q->where("o.status", 1);
            vMFLM:
            F2H5L:
        })->page($pageindex + 1, $pagesize)->orderby("o.id", "desc")->getall();
        $system = pdo_get("yzzc_sun_settings", array("uniacid" => $_W["uniacid"]));
        if ($data) {
            foreach ($data as $key => $val) {
                $data[$key]["total_money"] = $val["prepay_money"] . "/" . $val["xzmoney"];
            }
            echo json_encode($data);
        } else {
            echo 2;
        }
    }

    /**
     * @return void
     * 分销订单
     */
    public function doPageGetDistributionOrderLists()
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        //$ordertype = isset($_GPC["ordertype"]) ? $_GPC["ordertype"] : 1;
        $status = intval($_GPC["orderstatus"]);
        $uid = intval($_GPC["uid"]);
        $pagesize = 10;
        $pageindex = isset($_GPC["page"]) ? intval($_GPC["page"])  : 0;

        $query = load()->object("query");
        $data = $query->from("yzzc_sun_order", "o")->leftjoin("yzzc_sun_distribution_order", "os")->on("o.id", "os.order_id")->leftjoin("yzzc_sun_user", "u")->on("u.id", "o.uid")->leftjoin("yzzc_sun_goods", "car")->on("o.cid", "car.id")->select("u.user_name as name", "u.headimg as uthumb", "o.ordernum", "o.money", "o.day", "o.id", "os.first_price", "os.second_price", "os.third_price", "car.pic as carimg", "car.name as carname", "o.cid", "o.prepay_money", "o.xzmoney", "o.status")->where("o.uniacid", $uniacid)->where("o.type", 1)->where(function ($query) use($uid) {
            $query->whereor("os.parent_id_1", $uid);
            $query->whereor("os.parent_id_2", $uid);
            $query->whereor("os.parent_id_3", $uid);
        })->where(function ($q) use($status) {
            if ($status > 0) {
                goto CUaAc;
            }
            $q->where("o.id >", 0);
            goto F2H5L;
            CUaAc:
            if ($status == 2) {
                goto PGpYY;
            }
            if ($status == 3) {
                goto n0tKj;
            }
            if (!($status == 5)) {
                goto U6Ho0;
            }
            $q->where("o.status", 0);
            U6Ho0:
            goto NfPWe;
            n0tKj:
            $q->where("o.status >=", 2);
            $q->where("o.status <", 4);
            NfPWe:
            goto vMFLM;
            PGpYY:
            $q->where("o.status", 1);
            vMFLM:
            F2H5L:
        })->page($pageindex, $pagesize)->orderby("o.id", "desc")->getall();
        if ($data) {
            foreach ($data as $key => $val) {
                $data[$key]["total_money"] = $val["prepay_money"] . "/" . $val["xzmoney"];
            }
        }
        $this->return_msg(1,'操作成功',$data);
    }
    public function CountOrdermoney($parent, $uid = 0)
    {
        global $_W, $_GPC;
        $uniacid = $_W["uniacid"];
        $sql_mid = '';
        $where_p = '';
        $field = '';
        $where = " where d.uniacid=" . $uniacid . " and d.is_delete=0 and o.order_status=0 ";
        if ($parent == 1) {
            $where .= " and d.parent_id_1=" . $uid . " ";
            $field = " sum(d.first_price) as money ";
        } else {
            if ($parent == 2) {
                $where .= " and d.parent_id_2=" . $uid . " ";
                $field = " sum(d.second_price) as money ";
            } else {
                if ($parent == 3) {
                    $where .= " and d.parent_id_3=" . $uid . " ";
                    $field = " sum(d.third_price) as money ";
                } else {
                }
            }
        }
        $sql_ordinary = " select " . $field . " from " . tablename("yzzc_sun_distribution_order") . " as d left join " . tablename("yzzc_sun_order") . " as o on o.id=d.order_id " . $where;
        $money = pdo_fetch($sql_ordinary);
        if ($money) {
            return $money["money"];
        } else {
            return 0;
        }
    }
}